﻿//JumbotCms_4.1 Final
using System;
using System.Web;
using System.Data;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.MTag
{
    public partial class _view_invite_ajax : JumbotCms.API.UserCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CheckFormUrl())
            {
                Response.Write("提交有误");
                Response.End();
            }

            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxInvite":
                    ajaxInvite();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }
        private void DefaultResponse()
        {
            User_Load("", "json");
        }
        private void ajaxGetList()
        {
            User_Load("", "json");
            int page = Int_ThisPage();
            int PSize = Str2Int(q("pagesize"), 10);
            int countNum = 0;
            string sqlStr = "";
            string joinStr = "A.[FriendId]=B.Id";
            string whereStr1 = "A.Id>0";//外围条件(带A.)
            string whereStr2 = "Id>0";//分页条件(不带A.)
            whereStr1 += " AND A.UserId=" + UserId;
            whereStr2 += " AND UserId=" + UserId;
            doh.Reset();
            doh.ConditionExpress = whereStr2;
            countNum = doh.Count("jcms_normal_user_friends");
            sqlStr = Jumbot.Common.staticFunction.GetSql("A.id as id,A.FriendId as FriendUserId,B.UserName as FriendUserName,A.AddDate as AddDate,(select count(id) FROM [jcms_normal_mtag_member] WHERE UserId=a.FriendId AND MTagId=" + Str2Str(q("mtagid")) + ") as FriendIsMember", "jcms_normal_user_friends", "jcms_normal_user", "Id", PSize, page, "desc", joinStr, whereStr1, whereStr2);
            doh.Reset();
            doh.SqlCmd = sqlStr;
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\"," +
                "returnval :\"操作成功\"," +
                "pagerbar :\"" + Jumbot.Common.HtmlPager.GetPageBar(3, "js", 2, countNum, PSize, page, "javascript:ajaxList(<#page#>);") + "\"," +
                Jumbot.Common.JsonHelp.DtToSON(dt, (PSize * (page - 1))) +
                "}";
            dt.Clear();
            dt.Dispose();
        }
        /// <summary>
        /// 执行邀请好友
        /// </summary>
        private void ajaxInvite()
        {
            string ids = f("ids");
            string _mtagid = Str2Str(q("mtagid"));
            string[] idValue;
            idValue = ids.Split(',');
            for (int i = 0; i < idValue.Length; i++)
            {
                if (!IsMTagMember(idValue[i], _mtagid))
                {
                    doh.Reset();
                    doh.AddFieldItem("MTagId", _mtagid);
                    doh.AddFieldItem("Power", 0);
                    doh.AddFieldItem("UserId", idValue[i]);
                    doh.Insert("jcms_normal_mtag_member");
                    //统计加一
                    doh.Reset();
                    doh.ConditionExpress = "id=@id";
                    doh.AddConditionParameter("@id", _mtagid);
                    doh.Add("jcms_normal_mtag", "MemberTotal", 1);
                }
            }
            this._response = JsonResult(1, "操作成功");
        }
    }
}
